<?
$info = $GLOBALS['info'];
if (get_magic_quotes_gpc()){
	$GLOBALS['return_to'] = stripslashes_r($GLOBALS['return_to']);
}
$return_to_page = preg_replace('/:amp:/', '&', $GLOBALS['return_to']);
$return_to = preg_replace("/'/", "\'", $GLOBALS['return_to']);
$client_details = clientDetails($info['client_id']);

$params['client_id'] = $info['client_id'];
$options['limit'] = 1000;
$options['group_by'] = array('order_id', 'order_number', 'order_status', 'sale_number', 'sale_type', 'sale_date', 'sale_date_', 'client_id', 'client_country', 'client_firstname', 'client_lastname', 'client_username', 'sum_amount_total', 'sum_amount_wholesale', 'payment_currency', 'payment_exchange', 'client_vat_num', 'payment_method');
$invoices = getGeneralResults($params, $options);

if($info['order_id']){
	$params['order_id'] = $info['order_id'];
	$options['order_by_'] = 'ASC';
	$order_invoices = getGeneralResults($params, $options);
	//print_r($order_invoices);
}

?>
<table class='account_table' style="border-bottom:0px;" bgcolor="#fcfcee" cellspacing=0 cellpadding=0 width=100%>
  <tr>
	<th colspan="3" align="left"><font color="#000000">&nbsp;&nbsp;&raquo; ACCOUNT DETAILS</font></th>
  </tr>
  <tr>
	<td valign="top" style="padding-left:10px;">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="90">Username:</td>
        <td><strong class="red"><? echo $client_details['username']; ?></strong>&nbsp;<a href="<?=loginClient($client_details['username']);?>" target='_blank'><img src="images/btn_mini_login_01.gif" alt="Login" border="0" align="absmiddle"></a></td>
      </tr>
      <tr>
        <td>Account Status:</td>
        <td><?=($client_details['blocked'] == 1 ? '<strong class="red">Blocked</strong>' : '<strong style="color:#006600;">Active</strong>');?></td>
      </tr>
      <tr>
        <td>Creation Date:</td>
        <td><strong><? echo date('m/d/Y', strtotime($client_details['start_date'])); ?></strong></td>
      </tr>
      <tr>
        <td>Expiration Date:</td>
        <td><strong><? echo date('m/d/Y', strtotime($client_details['expire_date'])); ?><?/*</strong>&nbsp;<a href="#"><img align="absmiddle" src="images/btn_mini_renew_01.gif" alt="Renew" border="0"></a>*/?></td>
      </tr>
      <tr>
        <td>Server:</td>
        <td><a href="http://<? echo $client_details['server']; ?>" target="_blank"><strong><? echo $client_details['server']; ?></strong></a></td>
      </tr>
    </table>
	</td>
	<td width="20">&nbsp;</td>
	<td valign="top" style="padding-right:10px;">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="90">Company:</td>
        <td><strong><? echo $client_details['company_name']; ?></strong></td>
      </tr>
      <tr>
        <td>Contact Name:</td>
        <td><strong><? echo $client_details['firstname'].' '.$client_details['lastname']; ?></strong></td>
      </tr>
      <tr>
        <td valign="top">Address:</td>
		<td>
			<strong>
			<?
			echo $client_details['address1'].'<br>'.
			($client_details['address2'] ? $client_details['address2'].'<br>' : '').
			ucwords($client_details['city']).' '.
			strtoupper($client_details['state']).' '.
			strtoupper($client_details['zip']).'<br>'.
			$client_details['country_name'];
			?>
			</strong>
		</td>
      </tr>
      <tr>
        <td>Tel:</td>
        <td><strong><? echo $client_details['phone']; ?></strong></td>
      </tr>
      <tr>
        <td>E-mail:</td>
        <td><strong><? echo $client_details['email']; ?></strong></td>
      </tr>
    </table></td>
  </tr>
  <tr>
	<td>&nbsp;</td>
	<td></td>
	<td></td>
  </tr>
</table>
<table cellspacing=0 cellpadding=0 width=100% class="myTable">
  <tr>
    <td width='10' class='blank' bgcolor="#fcfcee">&nbsp;</td>
    <td class='selected' align="center"><b>Billing</b></td>
    <td width='10' class='blank' bgcolor="#fcfcee">&nbsp;</td>
    <td class='notselected' align="center" onClick="openPage('account(domains).html', 'info[client_id]=<?=$info[client_id]?>&info[order_id]=<?=$info[order_id]?>&return_to=' + encodeURI('<?=$return_to?>')); return false;"><b>Domains</b></td>
    <td width='10' class='blank' bgcolor="#fcfcee">&nbsp;</td>
<?/*
    <td class='notselected' align="center"><strong>Notifications</strong></td>
	<td width='10' class='blank' bgcolor="#fcfcee">&nbsp;</td>
    <td class='notselected' align="center"><b>Events</b></td>
    <td width='10' class='blank' bgcolor="#fcfcee">&nbsp;</td>
    <td class='notselected' align="center"><strong>Plan Details</strong></td>
    <td width='10' class='blank' bgcolor="#fcfcee">&nbsp;</td>
    <td class='notselected' align="center"><strong>Log</strong></td>
    <td width='10' class='blank' bgcolor="#fcfcee">&nbsp;</td>
*/?>
  </tr>
  <tr>
    <td colspan=13 class='main'>
    	<?
		if($info['order_id']){
		?>
    	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td align="center"><strong>BILLING DETAILS FOR ORDER ID</strong> <strong class="red"><?=$info[order_id]?></strong></td>
			<td align="right"></td>
		  </tr>
		</table>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td valign="top" width="49%">
				<table id="selection_tables" style="padding:0px; margin:0px; border:1px solid #84ccff;" width="100%" cellspacing="0" cellpadding="0">
				  <tr style="background:#b1dfff;">
					<td colspan="2" align="center"><strong>INVOICES</strong></td>
					<td align="center"><strong>AMOUNT</strong></td>
				  </tr>
				<?
				$invoice_details = $order_invoices['result'][0];
				$invoice_details['invoice_type'] = 'invoice';
				$invoice_details['index'] = '';
				$tmp = generate_invoice_number($invoice_details);
				$invoice_details['t_invoice_number'] = '#'.preg_replace('/x/', 'a', $tmp);
				$invoice_details['wh_invoice_number'] = '#'.preg_replace('/x/', 'c', $tmp);
				$invoice_details['retail'] = $GLOBALS['currencies'][($invoice_details['payment_currency'])].' '.$invoice_details['sum_amount_total'];
				$invoice_details['wholesale'] = $GLOBALS['currencies'][($invoice_details['payment_currency'])].' '.$invoice_details['sum_amount_wholesale'];

				$params['sid'] = $sid;
				$params['action'] = 'view_invoice';
				$params['invoice_number'] = substr($invoice_details['wh_invoice_number'], 1);
				$url_reseller = $GLOBALS['baseURL'].'/?'.encode_params($params);
				$params['invoice_number'] = substr($invoice_details['t_invoice_number'], 1);
				$url_client = $GLOBALS['baseURL'].'/?'.encode_params($params);
				?>
				  <tr>
					<td>&nbsp;&nbsp;Retail</td>
					<td><a href="<?=$url_client;?>" target="_blank"><strong><?=$invoice_details['t_invoice_number'];?></strong></a></td>
					<td align="right" nowrap style="padding-right:10px;"><strong><?=$invoice_details['retail'];?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Wholesale</td>
					<td><strong><a href="<?=$url_reseller;?>" target="_blank"><?=$invoice_details['wh_invoice_number'];?></a></strong></td>
					<td align="right" nowrap style="padding-right:10px;"><strong><?=$invoice_details['wholesale'];?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Date</td>
					<td colspan=2><strong><?=date('m/d/Y', strtotime($invoice_details['sale_date']));?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Gateway</td>
					<td colspan=2><strong><?=$GLOBALS['inv_p1'][($GLOBALS['p2'][($invoice_details['payment_method'])])];?></strong></td>
				  </tr>
				  <tr>
					<td nowrap>&nbsp;&nbsp;Transaction ID</td>
					<td colspan=2><strong><?=$invoice_details['order_number'];?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Order Type</td>
					<td colspan=2><strong><?=$invoice_details['sale_type'];?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Order Status</td>
					<td colspan=2><strong style="color:#006600;">Verified</strong></td>
				  </tr>
				</table>
			</td>
            <td>&nbsp;</td>
			<td valign="top" width="49%">
				<table id="selection_tables" style="padding:0px; margin:0px; border:1px solid #ff9494;" width="100%" cellspacing="0" cellpadding="0">
				  <tr style="background:#ffb8b5;">
					<td colspan="2" align="center"><strong>CREDIT NOTES</strong></td>
					<td align="center"><strong>AMOUNT</strong></td>
				  </tr>
				<?
				if(in_array($order_invoices['result'][0]['order_status'], array(16, 32))){
					echo "<tr>
						<td colspan=3 align='center'>No Credit Notes</td>
					</tr>";
				}
				foreach($order_invoices['result'] as $k => $v){
					//echo "sum: $v[sum_amount_total] | os: $v[order_status]<br>";
					if($v['sale_type'] == 'reversal'){
						$invoice_details = $v;
						$invoice_details['invoice_type'] = 'credit_note';
						preg_match('/ref[\d]+$/', $v['sale_number'], $matches);
						$invoice_details['index'] = preg_replace('/ref/', '', $matches[0]);
						$tmp = generate_invoice_number($invoice_details);
						$invoice_details['t_invoice_number'] = '#'.preg_replace('/x/', 'a', $tmp);
						$invoice_details['wh_invoice_number'] = '#'.preg_replace('/x/', 'c', $tmp);
						$invoice_details['retail'] = $GLOBALS['currencies'][($invoice_details['payment_currency'])].' '.$invoice_details['sum_amount_total'];
						$invoice_details['wholesale'] = $GLOBALS['currencies'][($invoice_details['payment_currency'])].' '.$invoice_details['sum_amount_wholesale'];

		                                $params['invoice_number'] = substr($invoice_details['wh_invoice_number'], 1);
        	                	        $url_reseller = $GLOBALS['baseURL'].'/?'.encode_params($params);
                	                	$params['invoice_number'] = substr($invoice_details['t_invoice_number'], 1);
                        	        	$url_client = $GLOBALS['baseURL'].'/?'.encode_params($params);
				?>
				  <tr>
					<td>&nbsp;&nbsp;Retail</td>
					<td><a href="<?=$url_client;?>" target="_blank"><strong class="red"><?=$invoice_details['t_invoice_number'];?></strong></a></td>
					<td align="right" nowrap style="padding-right:10px;"><strong><?=$invoice_details['retail'];?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Wholesale</td>
					<td><a href="<?=$url_reseller;?>" target="_blank"><strong class="red"><?=$invoice_details['wh_invoice_number'];?></strong></a></td>
					<td align="right" nowrap style="padding-right:10px;"><strong><?=$invoice_details['wholesale'];?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Date</td>
					<td colspan=2><strong><?=date('m/d/Y', strtotime($invoice_details['sale_date']));?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Gateway</td>
					<td colspan=2><strong><?=$GLOBALS['inv_p1'][($GLOBALS['p2'][($invoice_details['payment_method'])])];?></strong></td>
				  </tr>
				  <tr>
					<td nowrap>&nbsp;&nbsp;Transaction ID</td>
					<td colspan=2><strong><?=$invoice_details['order_number'];?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Order Type</td>
					<td colspan=2><strong><?=$invoice_details['sale_type'];?></strong></td>
				  </tr>
				  <tr>
					<td>&nbsp;&nbsp;Order Status</td>
					<td colspan=2><strong style="color:#990099;"><?=($invoice_details[order_status] == 1024 ? 'P.Refund' : 'F.Refund');?></strong></td>
				  </tr>
				<?
					}
				}
				?>
				</table>
			</td>
		  </tr>
		</table>
		<?
		}
		?>
		<br />
		<table class='bTable' cellspacing=0 cellpadding=0 width=100%>
			<tr>
				<th>Date</th>
				<th>Order ID</th>
				<th>Type</th>
				<th>Retail</th>
				<th>Wholesale </th>
			</tr>
			<?
			foreach($invoices['result'] as $k => $v){
					$date = date('m/d/Y', strtotime($v['sale_date']));
					$retail = $GLOBALS['currencies'][($v['payment_currency'])].' '.$v['sum_amount_total'];
					$wholesale = $GLOBALS['currencies'][($v['payment_currency'])].' '.$v['sum_amount_wholesale'];
					if($v['sale_type'] != 'reversal'){
						$type = 'Invoice';
					}else{
						$type = 'Credit Note';
						$retail = "<font color='red'>$retail</font>";
						$wholesale = "<font color='red'>$wholesale</font>";
					}
				echo "<tr>
					<td align='center'>$date</td>
					<td align='center'><a href='#' onClick=\"openPage('account(billing).html', 'info[order_id]=$v[order_id]&info[client_id]=$v[client_id]&return_to=' + encodeURI('$return_to')); return false;\">$v[order_id]</a></td>
					<td align='center'>$type</td>
					<td align='right' style='padding-right:20px;'>$retail</td>
					<td align='right' style='padding-right:20px;'>$wholesale</td>
				</tr>
				<tr>
					<td height='1' colspan='5'><div style='height:1px; background:url(images/dots_grey_line_01.gif); background-repeat:repeat-x;'></div></td>
				</tr>";
			}
			?>
		</table>
	</td>
  </tr>
  <tr>
	<td align='center' colspan=13>
		<input type='button' value='Go Back' onClick="<?=$return_to_page?>">
	</td>
  </tr>
</table>